package com.jumi.microservice.controller.logistics.admin;

import com.alibaba.excel.EasyExcel;
import com.jumi.microservice.common.base.BaseController;
import com.jumi.microservice.common.base.dto.ExportExcelCommonResponse;
import com.jumi.microservice.common.base.dto.ResponseResult;
import com.jumi.microservice.common.page.TableDataInfo;
import com.jumi.microservice.dto.order.OrderMessage;
import com.jumi.microservice.dto.logistics.*;
import com.jumi.microservice.dto.logistics.excel.ExcelSpiltMerge;
import com.jumi.microservice.service.logistics.AutoSpiltService;
import com.jumi.microservice.service.logistics.admin.MergeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.client.producer.SendStatus;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @author by mazhao
 * @Classname MergeController
 * @Description TODO
 * @Date 2020/8/10 16:57
 */
@RestController
@RequestMapping("/admin/logistics")
@Api(tags = {"拆合单相关接口"})
public class SpiltMergeController extends BaseController {

    @Resource
    MergeService mergeService;

    @Resource
    RocketMQTemplate rocketMQTemplate;

    @Resource
    AutoSpiltService autoSpiltService;

    @GetMapping("autoSpilt")
    void test(@RequestParam("orderNo") String orderNo){
        AutoSpiltRequest request = new AutoSpiltRequest();
        request.setOrderNo(orderNo);
        autoSpiltService.autoSpiltOrder(request);
    }

    @ApiOperation("拆合单页面列表")
    @PostMapping("mergeList")
    TableDataInfo<MergeListResponse> mergeList(MergeListRequest request){
       return  mergeService.list(request);
    }

    @ApiOperation("自动合单")
    @PostMapping("/autoMerge")
    ResponseResult<Boolean> autoMerge(MergeRequest request){
        return  success(mergeService.autoMerge(request));
    }

    @ApiOperation("单个确认包裹")
    @PostMapping("/spiltMergeSubmit")
    ResponseResult<Boolean> spiltMergeConfirm(@RequestBody MergeSubmitRequest request){
        return  success(mergeService.spiltMergeSubmit(request));
    }

    @ApiOperation("批量确认包裹")
    @PostMapping("/allSpiltMergeSubmit")
    ResponseResult<Boolean> allSpiltMergeConfirm( MergeRequest request){
        return  success(mergeService.allSpiltMergeSubmit(request));
    }


    @ApiOperation("编辑包裹确认提交")
    @PostMapping("/editSubmit")
    ResponseResult<Boolean> editConfirm(@RequestBody MergeSubmitRequest request){
        return  success(mergeService.editConfirm(request));
    }

    @ApiOperation("获取允许添加到包裹列表")
    @PostMapping("/allAddPackageList")
    ResponseResult<List<PackageInfo>> getAllowAddGoodsPackageInfoList(@RequestBody MergeAllowAddRequest request){
        return  success(mergeService.getAllowAddGoodsPackageInfoList(request));
    }

    @ApiOperation("生成包裹号")
    @PostMapping("/getLogisticsNo")
    ResponseResult<String> getLogisticsNo(){
        return  success(mergeService.getLogisticsNo());
    }

    @ApiOperation("手动拆合单导出")
    @PostMapping("/export")
    void export(HttpServletResponse response, MergeListRequest request) throws IOException {
        ExportExcelCommonResponse<ExcelSpiltMerge> exportExcelCommonResponse = mergeService.excelExport(request);
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-disposition", "attachment;filename="+ exportExcelCommonResponse.getFileName());
        EasyExcel.write(response.getOutputStream(), ExcelSpiltMerge.class).sheet("sheet1").doWrite(exportExcelCommonResponse.getData());
    }

    @ApiOperation("手动拆合单导入")
    @PostMapping("/import")
    public ResponseResult<List<ExcelSpiltMerge>> spiltMergeImport(HttpServletRequest request, MergeListRequest mergeListRequest) throws IOException {
        return success(mergeService.excelImport(request,mergeListRequest));
    }

    @ApiOperation("拆合单分类字典")
    @PostMapping("/spiltMergeCategory")
    ResponseResult<Map<Integer,String>> spiltMergeCategory(){
        return success(mergeService.spiltMergeCategoryList());
    }

}
